package com.cow.controller;

import com.cow.entity.Order;
import com.cow.service.OrderService;
import com.cow.util.general.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;


    @PostMapping("/cartOrder")
    public CommonResult cartOrder(@RequestBody List<Order> orders) {
//        List<Order> orders = JSON.parseArray(ordersInfo, Order.class);
//        List<Integer> cartIdList = JSON.parseArray(cartIds, Integer.class);
//        System.out.println(orderService);

        boolean b = orderService.handleCartOrder(orders);
        if (b) {
            return CommonResult.success("下单成功");
        }
        return CommonResult.error("下单失败");
    }

    @PostMapping("/add")
    public CommonResult add(Order order) {

        boolean b = orderService.add(order);
        if (b) {
            return CommonResult.success("下单成功");
        }
        return CommonResult.error("下单失败");
    }

    @GetMapping("/findAccountOrder")
    public CommonResult findAccountOrder(String userAccount){
        List<Map<String, Object>> mapList = orderService.selectOrdersByUserAccount(userAccount);
        return CommonResult.success("订单查询成功",mapList);
    }

}
